package com.fable.sp.core.template.freemarker;

import com.fable.sp.core.util.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * .
 * <p/>
 *
 * @author <a href="mailto:stormning@163.com">stormning</a>
 * @version V1.0, 2015/8/15.
 */
@Component
public class Env {
    private static Environment environment;

    @Autowired
    public Env(Environment environment) {
        Env.environment = environment;
    }

    private Env() {
    }

    public static boolean isDev() {
        return "dev".equals(environment.getProperty("project.mode"));
    }

    public static String url(String url) {
        if (url.startsWith("http")) {
            return url;
        }
        return baseUrl() + format(url);
    }

    public static String baseUrl() {
        return StringUtils.defaultString(environment.getProperty("project.baseUrl"), HttpUtil.getContextPath());
    }

    private static String format(String url) {
        return url.charAt(0) == '/' ? url : "/" + url;
    }

    public static Environment getEnvironment() {
        return environment;
    }
}
